﻿#include <QAuthenticator>
#include <QNetworkReply>
#include "CIsapiClient.h"
#include "CGlobal.h"

CIsapiClient::CIsapiClient(const QString &username, const QString &password, QObject *parent /*= nullptr*/)
	: QObject(parent), m_sUsernName(username), m_sPassword(password), m_pManager(new QNetworkAccessManager(this)) 
{
	connect(m_pManager, &QNetworkAccessManager::authenticationRequired, this, &CIsapiClient::slotAuthenticationRequired);
}

CIsapiClient::CIsapiClient()
{
	m_pManager=new QNetworkAccessManager(this);
	connect(m_pManager, &QNetworkAccessManager::authenticationRequired, this, &CIsapiClient::slotAuthenticationRequired);
}

CIsapiClient::~CIsapiClient()
{
	if (m_pManager != nullptr)
	{
		delete m_pManager;
		m_pManager = nullptr;
	}
}

void CIsapiClient::setData(QString sUserName, QString sPasswd)
{
	m_sUsernName = sUserName;
	m_sPassword = sPasswd;
}

void CIsapiClient::sendRequestGet(int nRequestType, QUrl &url)
{
	QNetworkRequest request(url);
	QNetworkReply *reply = m_pManager->get(request);
	connect(reply, &QNetworkReply::finished, [this, reply, nRequestType]()
	{
		SRequestResult result;
		QString sErrMsg = "";
		if (reply->error() == QNetworkReply::NoError)
		{
			result.bResult = true;
			result.sResult = reply->readAll();
			result.sErrorMessage = A2T("请求成功");
		}
		else
		{
			switch (reply->error())
			{
			case QNetworkReply::TimeoutError:
				sErrMsg = A2T("请求超时");
				break;
			case QNetworkReply::HostNotFoundError:
				sErrMsg = A2T("找不到主机");
				break;
			default:
				sErrMsg = A2T("请求失败");
				break;
			}
			result.bResult = false;
			result.sResult = "";
			result.sErrorMessage = sErrMsg;
		}

		// 使用枚举类型作为标识符
		emit sigResult(nRequestType, result);
		reply->deleteLater();
	});
}

void CIsapiClient::sendRequestGet(int nRequestType, QString sParam, QUrl &url)
{
	QNetworkRequest request(url);
	QNetworkReply *reply = m_pManager->get(request);
	connect(reply, &QNetworkReply::finished, [this, reply, nRequestType, sParam]()
	{
		SRequestResult result;
		QString sErrMsg = "";
		if (reply->error() == QNetworkReply::NoError)
		{
			result.bResult = true;
			result.sResult = reply->readAll();
			result.sErrorMessage = A2T("请求成功");
		}
		else
		{
			switch (reply->error())
			{
			case QNetworkReply::TimeoutError:
				sErrMsg = A2T("请求超时");
				break;
			case QNetworkReply::HostNotFoundError:
				sErrMsg = A2T("找不到主机");
				break;
			default:
				sErrMsg = A2T("请求失败");
				break;
			}
			result.bResult = false;
			result.sResult = "";
			result.sErrorMessage = sErrMsg;
		}

		// 使用枚举类型作为标识符
		emit sigResultParam(nRequestType, sParam, result);
		reply->deleteLater();
	});
}

void CIsapiClient::sendRequestPost(int nRequestType, QUrl &url, QByteArray postData)
{
	QNetworkRequest request(url);
	request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
	QNetworkReply *reply = m_pManager->post(request, postData);
	QString sErr = reply->errorString();
	connect(reply, &QNetworkReply::finished, [this, reply, nRequestType]()
	{
		SRequestResult result;
		QString sErrMsg = "";
		if (reply->error() == QNetworkReply::NoError)
		{
			result.bResult = true;
			result.sResult =reply->readAll();
			result.sErrorMessage = A2T("请求成功");
		}
		else 
		{
			switch (reply->error())
			{
			case QNetworkReply::TimeoutError:
				sErrMsg = A2T("请求超时");
				break;
			case QNetworkReply::HostNotFoundError:
				sErrMsg = A2T("找不到主机");
				break;
			default:
				sErrMsg = A2T("请求失败");
				break;
			}
			result.bResult = false;
			result.sResult = "";
			result.sErrorMessage = sErrMsg;
		}

		// 使用枚举类型作为标识符
		emit sigResult(nRequestType, result);
		reply->deleteLater();
	});
}

void CIsapiClient::slotAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
{
	authenticator->setUser(m_sUsernName);
	authenticator->setPassword(m_sPassword);
}
